/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Embarques.java
 *
 * Created on 24/08/2011, 11:14:32
 */

package View.ProducaoValores;

import Controle.Controlador;
import Controle.Uteis;
import Formulario.DadosEmbarque;
import Formulario.Formulario;
import Modelo.Cliente;
import Modelo.Cliente_has_Seguro;
import Modelo.Fisico;
import Modelo.Juridico;
import Modelo.Seguro;
import View.Fundo2;
import View.Progress;
import View.TelaMetodos;
import java.awt.BorderLayout;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Silvio
 */
public class Embarques extends javax.swing.JFrame {
    private static Cliente cliente;
    public static Fundo2 pnlFundo = new Fundo2();
    /** Creates new form Embarques */
    public Embarques() {
        initComponents();
        this.setLayout(new BorderLayout());
        this.add(pnlFundo, BorderLayout.CENTER);
        this.setExtendedState(MAXIMIZED_BOTH);
       
    }
    public Embarques(Cliente cli){
        this();
        cliente = cli;
        if(cli instanceof Juridico)
            preenche((Juridico)cli);
        else if(cli instanceof Fisico)
            preenche((Fisico)cli);
        emitente.setText(Controlador.getEmp().getRazao());
      
        desconto.setText(Uteis.Num(cli.getDesconto()));
    }
    public Embarques(Cliente cli, String dataInicio,String dataFim){
        this(cli);
        this.dataInicio.setText(dataInicio);
        this.dataFim.setText(dataFim);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel22 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tabelaProducao = new javax.swing.JTable();
        dataInicio = new javax.swing.JFormattedTextField();
        jButton1 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        desconto = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        Total = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        dataFim = new javax.swing.JFormattedTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel13 = new javax.swing.JLabel();
        soma = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        tLanc = new javax.swing.JTextField();
        valorMerca = new javax.swing.JTextField();
        vICMS = new javax.swing.JTextField();
        tSeguro = new javax.swing.JTextField();
        vOutros = new javax.swing.JTextField();
        vRecebido = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        valorDesconto = new javax.swing.JTextField();
        barradeprogresso = new javax.swing.JProgressBar();
        label = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        emitente = new javax.swing.JTextField();
        responsavel = new javax.swing.JTextField();
        jLabel17 = new javax.swing.JLabel();
        rtfc = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel22.setBackground(new java.awt.Color(204, 0, 0));
        jLabel22.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel22.setForeground(new java.awt.Color(204, 0, 0));
        jLabel22.setText("RESUMO DE EMBARQUES");

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/voltar.png"))); // NOI18N
        jButton3.setText("Voltar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Contato", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 14), new java.awt.Color(204, 0, 0))); // NOI18N
        jPanel2.setPreferredSize(new java.awt.Dimension(900, 636));

        jLabel1.setText("Responsável:");

        jLabel2.setText("Data Inicio:");

        tabelaProducao.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tabelaProducao.addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                tabelaProducaoAncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });
        jScrollPane2.setViewportView(tabelaProducao);

        dataInicio.setFormatterFactory((new javax.swing.text.DefaultFormatterFactory(Uteis.Mascara("##/##/####"))));

        jButton1.setText("buscar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel3.setText("Emitente:");

        jLabel4.setText("Descontos %:");

        desconto.setDisabledTextColor(java.awt.Color.black);
        desconto.setEnabled(false);

        jLabel5.setText("Soma Total:");

        jLabel7.setText("Valor a Pagar:");

        jLabel6.setText("Data Fim:");

        dataFim.setFormatterFactory((new javax.swing.text.DefaultFormatterFactory(Uteis.Mascara("##/##/####"))));

        jLabel9.setText("Total valor de mercadorias:");

        jLabel10.setText("Valor total de seguro:");

        jLabel11.setText("Valor Total Recebido:");

        jLabel12.setText("Valor Total de ICMS:");

        jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);

        jLabel13.setText("Valor total de outros:");

        jLabel14.setText("Total de Lançamentos:");

        jLabel15.setText("Valor Desconto:");

        jLabel16.setText("Gerando Comprovante:");

        emitente.setEditable(false);
        emitente.setDisabledTextColor(java.awt.Color.black);
        emitente.setEnabled(false);
        emitente.setOpaque(false);

        responsavel.setBackground(new java.awt.Color(255, 255, 255));
        responsavel.setEditable(false);
        responsavel.setDisabledTextColor(java.awt.Color.black);
        responsavel.setEnabled(false);

        jLabel17.setText("valor total do RTFC:");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(532, 532, 532)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(12, 12, 12)
                        .addComponent(dataInicio, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel1)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 889, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel9)
                            .addComponent(jLabel12)
                            .addComponent(jLabel10)
                            .addComponent(jLabel13)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(2, 2, 2)
                                .addComponent(jLabel11)))
                        .addGap(4, 4, 4)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(valorMerca, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(vICMS, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tSeguro, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(vOutros, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(vRecebido, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(34, 34, 34)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel14)
                                        .addGroup(jPanel2Layout.createSequentialGroup()
                                            .addGap(23, 23, 23)
                                            .addComponent(jLabel17))
                                        .addGroup(jPanel2Layout.createSequentialGroup()
                                            .addGap(44, 44, 44)
                                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                .addComponent(jLabel5)
                                                .addComponent(jLabel15))))
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addComponent(jLabel4)
                                        .addGap(5, 5, 5))))
                            .addComponent(jLabel7))
                        .addGap(13, 13, 13)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(Total)
                            .addComponent(valorDesconto)
                            .addComponent(soma)
                            .addComponent(tLanc, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)
                            .addComponent(rtfc, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)
                            .addComponent(desconto, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE))
                        .addGap(25, 25, 25)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel16)
                            .addComponent(barradeprogresso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(label, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                                .addComponent(responsavel, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                                .addComponent(emitente, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(353, 353, 353)
                                .addComponent(jLabel6)
                                .addGap(35, 35, 35)
                                .addComponent(dataFim, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addGap(93, 93, 93))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(3, 3, 3)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(5, 5, 5)
                        .addComponent(jLabel2))
                    .addComponent(dataInicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(emitente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(17, 17, 17)
                        .addComponent(jLabel6))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(dataFim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(1, 1, 1)
                .addComponent(jLabel1)
                .addGap(1, 1, 1)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(responsavel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 272, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel9)
                        .addGap(24, 24, 24)
                        .addComponent(jLabel12)
                        .addGap(24, 24, 24)
                        .addComponent(jLabel10)
                        .addGap(31, 31, 31)
                        .addComponent(jLabel13)
                        .addGap(21, 21, 21)
                        .addComponent(jLabel11))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(valorMerca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(vICMS, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(tSeguro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(25, 25, 25)
                        .addComponent(vOutros, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(vRecebido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addComponent(jLabel16)
                        .addGap(28, 28, 28)
                        .addComponent(barradeprogresso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(12, 12, 12)
                        .addComponent(label, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(3, 3, 3)
                                .addComponent(tLanc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(12, 12, 12)
                                .addComponent(rtfc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(12, 12, 12)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(desconto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel4))
                                .addGap(8, 8, 8)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel5)
                                    .addComponent(soma, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(8, 8, 8)
                                .addComponent(jLabel14)
                                .addGap(22, 22, 22)
                                .addComponent(jLabel17)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel15)
                            .addComponent(valorDesconto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(Total, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/visualizar.png"))); // NOI18N
        jButton2.setText("Gerar Relatorio");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 1019, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel22)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 333, Short.MAX_VALUE)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(83, 83, 83)
                        .addComponent(jButton2)
                        .addGap(127, 127, 127))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel22)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton2)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 634, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(69, 69, 69))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
              TelaMetodos.Dispose(this);

}//GEN-LAST:event_jButton3ActionPerformed

    private void tabelaProducaoAncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_tabelaProducaoAncestorAdded

        tabelaProducao.setModel(tabela);
        tabela.addColumn("DATA");
        tabela.addColumn("STRC");
        tabela.addColumn("UF\n ORIGEM");
        tabela.addColumn("UF\n DESTINO");
        tabela.addColumn("MERCADORIA");
        tabela.addColumn("VALOR DA \n MERCADORIA");
        tabela.addColumn("PLACA");
        tabela.addColumn("ICMS");
        tabela.addColumn("SEGURO");
        tabela.addColumn("OUTROS");
        tabela.addColumn("TOTAL");
        tabela.addColumn("RECEBIDO");
        tabela.addColumn("DIFERENÇA");
}//GEN-LAST:event_tabelaProducaoAncestorAdded

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
          final Progress p = new Progress();
          p.setVisible(true);
          SwingWorker worker = new SwingWorker(){
               @Override
               protected Object doInBackground() throws Exception {
                    preenche(); // meu processo demorado
                    return null;
               }
               @Override
               protected void done() {
                    p.setVisible(false);
               }
          };
          worker.execute();
    }//GEN-LAST:event_jButton1ActionPerformed

    public void preenche(){
        totalFatura =0;
        totalRecebido =0;
        BuscaSeguros();
        TelaMetodos.defineRenderers(tabelaProducao);
    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        
            if(tabelaProducao.getRowCount()>0){
                //public DadosEmbarque(DefaultTableModel tabela, String emitente, String responsavel, String periodo, String desconto, String valorDesconto, String soma, String valorFinal, String somaMercadoria,
                DadosEmbarque e = new DadosEmbarque(tabela,emitente.getText(),responsavel.getText(),Uteis.periodo(dataFim.getText()),desconto.getText(),valorDesconto.getText(),soma.getText(),Total.getText(),valorMerca.getText(),
                    //String somaICMS, String somaOutros, String somaTotal, String somaRecebido, String somaDiferenca, String valorSemDesc,String SomaSeguro) {
                        vICMS.getText(),vOutros.getText(),Uteis.Moeda(totalFatura),Uteis.Moeda(totalRecebido),Uteis.Moeda((totalFatura-totalRecebido)),soma.getText(),tSeguro.getText());
              
            if(processo !=null && processo.getState()== processo.getState().TERMINATED)
                    processo = null;
            executaProcesso(e);

            }else{
                JOptionPane.showMessageDialog(null, "Não existe dados na tabela");
            }
       
    }//GEN-LAST:event_jButton2ActionPerformed

    private void executaProcesso(DadosEmbarque emb) {
      // if(!processo.isAlive())
        //        processo=null;
        if(processo==null) { //Instancia a thread SE não existir uma
            processo = new Thread(new Formulario(emb,barradeprogresso,label,2));
            processo.start();   
        } else {
            label.setText("O processo ainda está em execução");
        }
    }


    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Embarques().setVisible(true);
            }
        });
    }
   private Thread processo;
   private DefaultTableModel tabela = new DefaultTableModel();
   private ArrayList <Cliente_has_Seguro> cli = new ArrayList <Cliente_has_Seguro>();
   private float  tempF=0,tempM=0,tempI=0,tempS=0,tempO=0,tempR=0;
   private float totalFatura,totalRecebido,totalDiferenca;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField Total;
    private javax.swing.JProgressBar barradeprogresso;
    private javax.swing.JFormattedTextField dataFim;
    private javax.swing.JFormattedTextField dataInicio;
    private javax.swing.JTextField desconto;
    private javax.swing.JTextField emitente;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel label;
    private javax.swing.JTextField responsavel;
    private javax.swing.JTextField rtfc;
    private javax.swing.JTextField soma;
    private javax.swing.JTextField tLanc;
    private javax.swing.JTextField tSeguro;
    private javax.swing.JTable tabelaProducao;
    private javax.swing.JTextField vICMS;
    private javax.swing.JTextField vOutros;
    private javax.swing.JTextField vRecebido;
    private javax.swing.JTextField valorDesconto;
    private javax.swing.JTextField valorMerca;
    // End of variables declaration//GEN-END:variables

    private void preenche(Fisico fisico) {
       responsavel.setText(fisico.getPessoa().getNome());
    }

    private void preenche(Juridico juridico) {
        responsavel.setText(juridico.getNomeFantasia());
    }

    private void colocarDados(Seguro seg,Cliente_has_Seguro c) {
        String destino="",origem="";
        if(seg.getDest().getCli() instanceof Juridico){
            Juridico j = (Juridico) seg.getDest().getCli();
            destino = j.getPessoa().getEstado();
        }else if(seg.getDest().getCli() instanceof Fisico){
            Fisico f = (Fisico) seg.getDest().getCli();
            destino = f.getPessoa().getEstado();
        }


        if(seg.getRem().getCli() instanceof Juridico){
            Juridico j = (Juridico) seg.getRem().getCli();
            origem = j.getPessoa().getEstado();
        }else if(seg.getRem().getCli() instanceof Fisico){
            Fisico f = (Fisico) seg.getRem().getCli();
            origem = f.getPessoa().getEstado();
        }

        tabela.addRow(new String[]{seg.getDataSeguro().Formata_Data(),seg.getSTRC(),origem,destino,seg.getMer().getNome(),Uteis.Moeda(seg.getMer().getValorMercadoria())
        , seg.getVeic().getPlaca(),Uteis.Moeda(seg.getValorICMS()),Uteis.Moeda(seg.getValorSeguro()),Uteis.Moeda(seg.getOutros()),Uteis.Moeda(seg.getTotal()),Uteis.Moeda(c.getValorJaPago()),Uteis.Moeda((c.getValorFatura()-c.getValorJaPago()))
        });
      
       tabela.fireTableDataChanged();
    }
    private void ColocarSomas(float valorFaturado,float valorPago, float diferenca){
        tabela.addRow(new String[]{"","","","","","","","","","","","",""});
        tabela.addRow(new String[]{"","","","","","","","","","","Faturado  ","Valor Pago ","Diferença "});
        tabela.addRow(new String[]{"","","","","","","","","","",Uteis.Moeda(valorFaturado),Uteis.Moeda(valorPago),Uteis.Moeda(diferenca)});
        tabela.addRow(new String[]{"","","","","","","","","","","","",""});
    }

    private void BuscaSeguros() {
       int mes=0;
       this.tempF=0;this.tempM=0;this.tempI=0;this.tempS=0;this.tempO=0;this.tempR=0;
       float tempF=0,tempD=0,tempR=0;
        cli.clear();
       tempF=0;tempM=0;tempI=0;tempS=0;tempO=0;tempR=0;
       TelaMetodos.RemoveDadosTabela(tabela, tabelaProducao);
       cli.addAll(Controlador.buscarSegurosData(cliente,dataInicio.getText(),dataFim.getText()));
       if(cli.size()>0)
            mes = cli.get(0).getSeg().getDataSeguro().getMes();
       if(cli !=null){
             for(Cliente_has_Seguro c:cli){
                 if(mes == c.getSeg().getDataSeguro().getMes()){
                     tempD += (c.getValorFatura()-c.getValorJaPago());
                     tempF += c.getValorFatura();
                     tempR+=c.getValorJaPago();
                 }else {
                     mes = c.getSeg().getDataSeguro().getMes();
                     ColocarSomas(tempF,tempR,tempD);                    
                     tempF =0;
                     tempD =0;
                     tempR =0;
                     tempD += (c.getValorFatura()-c.getValorJaPago());
                     tempF += c.getValorFatura();
                     tempR+=c.getValorJaPago();
                 }
                 tempM+=c.getSeg().getMer().getValorMercadoria();
                 tempI+=c.getSeg().getValorICMS();
                 tempS+=c.getSeg().getValorSeguro();
                 tempO+=c.getSeg().getOutros();
                 this.tempR+= c.getValorJaPago();
                 this.tempF += (c.getValorFatura()-c.getValorJaPago());
                 colocarDados(c.getSeg(),c);
                 totalFatura += c.getValorFatura();
                 totalRecebido+= c.getValorJaPago();
           }
           ColocarSomas(tempF,tempR,tempD);
           Total.setText(Uteis.Moeda(cliente.calculoTotalSeguros(this.tempF)));
           soma.setText(Uteis.Moeda(this.tempF));
           valorMerca.setText(Uteis.Moeda(tempM));
           vICMS.setText(Uteis.Moeda(tempI));
           tSeguro.setText(Uteis.Moeda(this.tempS));
           vOutros.setText(Uteis.Moeda(tempO));
           vRecebido.setText(Uteis.Moeda(this.tempR));
           tLanc.setText(Uteis.NumInt(cli.size()));
           valorDesconto.setText(Uteis.Moeda(cliente.valorDesconto(this.tempF)));
           if(cli.size()>0)              
               rtfc.setText(Controlador.somaRTFC(this.cli));
       }
    }
}
